package com.rcm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rcm.dao.StudentinfoMapper;
import com.rcm.dao.TeacherinfoMapper;
import com.rcm.model.Studentinfo;
import com.rcm.model.Teacherinfo;
import com.rcm.utility.MyBatisTool;

/**
 * Servlet implementation class AddTeacherServlet
 */
@WebServlet("/Admininfocontroller/*")
public class Admininfocontroller extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private TeacherinfoMapper mapper4=null;

	//获取url
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String uri = request.getRequestURI();
		int idx = uri.lastIndexOf("/");
		String methodName = uri.substring(idx + 1);
		System.out.println(methodName);

		try {
			String adminname= (String) request.getSession().getAttribute("adminname");
			if(adminname!=null) {
			Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
					HttpServletResponse.class);
			method.invoke(this, request, response);
			}else {
				out.print("<script language='javascript'>alert('请重新登录账号!');window.location.href='http://localhost:8080/ElectiveSystem/index.jsp';</script>");
				return;
			}


		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	//添加教师
	protected void AddTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			SqlSession session=MyBatisTool.getSession();
			TeacherinfoMapper mapper4 = session.getMapper(TeacherinfoMapper.class);
			Map<String, String[]> map = request.getParameterMap();
			Teacherinfo teacherinfo = new Teacherinfo(null, null, null, null, null);
			BeanUtils.populate(teacherinfo, map);
			System.out.println(teacherinfo);
			mapper4.insert(teacherinfo);
			Teacherlist(request,response);
			session.close();

		} catch (Exception e) {
			response.sendRedirect("/error.jsp");
			return;
		}

	}
	//所有教师列表
	protected void Teacherlist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		SqlSession session=MyBatisTool.getSession();
		TeacherinfoMapper mapper4 = session.getMapper(TeacherinfoMapper.class);
		int pageNum=1;
		if (request.getParameter("pageNum")!=null) {
			pageNum=Integer.parseInt(request.getParameter("pageNum"));
		}

		PageHelper.startPage(pageNum,10);
		List<Teacherinfo> list4 = mapper4.selectAll();

		PageInfo<Teacherinfo> pageInfo=new  PageInfo<Teacherinfo>(list4);
		System.out.println(list4);
		session.close();
		request.setAttribute("pageInfo", pageInfo);
		request.getRequestDispatcher("/admininfo.jsp").forward(request, response);


	}

	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{

		SqlSession session=MyBatisTool.getSession();
		TeacherinfoMapper mapper4 = session.getMapper(TeacherinfoMapper.class);
		String tid=request.getParameter("tid");
		mapper4.deleteByPrimaryKey(tid);
		session.close();
		Teacherlist(request,response);

	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
